<?php
namespace backend\models\article;

use yii\db\ActiveRecord;
use yii\web\UploadedFile;
class Article extends ActiveRecord
{
    /**
     * @return string 返回该AR类关联的数据表名
     */
    public static function tableName()
    {
        return 'edu_article';
    }
   
    
    /** 
     * @inheritdoc 
     */  
    public function rules()  
    {  
        return [  
            [['article_title', 'article_descripte','article_contents','user_id','user_name','article_author','article_addtime'], 'required'],    
            [['article_title'],'string', 'max' => 255],
            [['article_thumb'],'file','extensions' => 'gif, jpg,png'],
        ];  
    }


    /** 
     * @inheritdoc 资讯首页
     */  
    public function getArticle($pages)  
    {  
        return $this->find()
                ->select(["us.*,GROUP_CONCAT(gs.artlable_name SEPARATOR ' / ') AS artlable_name"])
                ->from("edu_article as us")
                ->leftjoin("edu_article_lable as ug","us.article_id = ug.article_id")
                ->leftjoin("edu_artlable as gs","ug.artlable_id = gs.artlable_id")
                ->groupby("ug.article_id")
                ->orderby("article_addtime desc")
                ->offset($pages->offset)
                ->limit($pages->limit)
                ->asarray()
                ->all();
    }

    

	/** 
     * @inheritdoc 资讯首页
     */  
    public function getArticlesearch($article_id)  
    {  
        return $this->find()
                ->select(["us.*,GROUP_CONCAT(gs.artlable_name SEPARATOR ' / ') AS artlable_name"])
                ->from("edu_article as us")
                ->leftjoin("edu_article_lable as ug","us.article_id = ug.article_id")
                ->leftjoin("edu_artlable as gs","ug.artlable_id = gs.artlable_id")
                //->groupby("ug.article_id")
                ->orderby("article_addtime desc")
                ->where("us.article_id in ('$article_id)')")
                ->asarray()
                ->all();
    }

    /** 
     * @inheritdoc 资讯编辑
     */  
    public function Article()  
    {  
        return $this->find()
                ->select("*")
                ->from("edu_article as us")
                ->leftjoin("edu_article_lable as ug","us.article_id = ug.article_id")
                ->leftjoin("edu_artlable as gs","ug.artlable_id = gs.artlable_id")
                ->asarray()
                ->all();
    }

    /** 
     * @inheritdoc 资讯搜索
     */  
    public function ArtSearch($pages,$label_name,$code)  
    {  
        return $this->find()
                ->select(["us.*,GROUP_CONCAT(gs.artlable_name SEPARATOR ' / ') AS artlable_name"])
                ->from("edu_article as us")
                ->leftjoin("edu_article_lable as ug","us.article_id = ug.article_id")
                ->leftjoin("edu_artlable as gs","ug.artlable_id = gs.artlable_id")
                ->where("artlable_name like '%$label_name%' $code")
                ->groupby("ug.article_id")
                ->orderby("article_addtime desc")
                ->offset($pages->offset)
                ->limit($pages->limit)
                ->asarray()
                ->all();
    }

    /**
     * @inheritdoc 搜索数量
     */
    public function ArtCount($label_name,$code)
    {
        return $this->find()
                ->select(["*"])
                ->from("edu_article as us")
                ->leftjoin("edu_article_lable as ug","us.article_id = ug.article_id")
                ->leftjoin("edu_artlable as gs","ug.artlable_id = gs.artlable_id")
                ->where("artlable_name like '%$label_name%' $code")
                ->count();
    }

    /** 
     * @inheritdoc 前台资讯热门文章
     */  
    public function PopularArticles()  
    {  
        return $this->find()
                    ->select("article_id,article_title,article_count")
                    ->from("edu_article")
                    ->where("is_show")
                    ->orderby("article_count desc")
                    ->limit(5)
                    ->asarray()
                    ->all();
    }

    /** 
     * @inheritdoc 资讯首页
     */  
    public function Informations($pages)  
    {  
        return $this->find()
                ->select(["us.*,GROUP_CONCAT(gs.artlable_name SEPARATOR ' / ') AS artlable_name"])
                ->from("edu_article as us")
                ->leftjoin("edu_article_lable as ug","us.article_id = ug.article_id")
                ->leftjoin("edu_artlable as gs","ug.artlable_id = gs.artlable_id")
                ->where("us.is_show=1")
                ->groupby("ug.article_id")
                ->orderby("us.article_addtime desc")
                ->offset($pages->offset)
                ->limit($pages->limit)
                ->asarray()
                ->all();
    }

    /** 
     * @inheritdoc 前台文章搜索总数
     */  
    public function ArticleCount($keywords)  
    {  
        return $this->find()
                    ->select(["us.*,GROUP_CONCAT(gs.artlable_name SEPARATOR ' / ') AS artlable_name"])
                    ->from("edu_article as us")
                    ->leftjoin("edu_article_lable as ug","us.article_id = ug.article_id")
                    ->leftjoin("edu_artlable as gs","ug.artlable_id = gs.artlable_id")
                    ->where("us.is_show=1 and gs.artlable_id=$keywords")
                    ->count();
    }

    /** 
     * @inheritdoc 资讯首页
     */  
    public function InforSearch($pages,$keywords)  
    {  
        return $this->find()
                ->select(["us.*,GROUP_CONCAT(gs.artlable_name SEPARATOR ' / ') AS artlable_name"])
                ->from("edu_article as us")
                ->leftjoin("edu_article_lable as ug","us.article_id = ug.article_id")
                ->leftjoin("edu_artlable as gs","ug.artlable_id = gs.artlable_id")
                ->where("us.is_show=1 and gs.artlable_id=$keywords")
                ->groupby("ug.article_id")
                ->orderby("article_addtime desc")
                ->offset($pages->offset)
                ->limit($pages->limit)
                ->asarray()
                ->all();
    }

    /** 
     * @inheritdoc 资讯详情页
     */  
    public function Details($id)  
    {  
        return $this->find()
                ->select(["us.*,GROUP_CONCAT(gs.artlable_name SEPARATOR ' / ') AS artlable_name"])
                ->from("edu_article as us")
                ->leftjoin("edu_article_lable as ug","us.article_id = ug.article_id")
                ->leftjoin("edu_artlable as gs","ug.artlable_id = gs.artlable_id")
                ->where("us.is_show=1 and us.article_id=$id")
                ->groupby("ug.article_id")
                ->orderby("article_addtime desc")
                ->asarray()
                ->one();
    }

    /** 
     * @inheritdoc 资讯上篇
     */  
    public function Prev($article_id)  
    {  
        return $this->find()
                ->select("article_id,article_title")
                ->from("edu_article")
                ->where("article_id<$article_id and is_show=1")
                ->orderby("article_id desc")
                ->limit(1)
                ->asarray()
                ->one();
    }

    /** 
     * @inheritdoc 资讯下篇
     */  
    public function Next($article_id)  
    {  
        return $this->find()
                ->select("article_id,article_title")
                ->from("edu_article")
                ->where("article_id>$article_id and is_show=1")
                ->orderby("article_id asc")
                ->limit(1)
                ->asarray()
                ->one();
    }

}
?>